package cn.iocoder.lw.module.core.controller.app.evaluation;

import cn.iocoder.lw.framework.common.pojo.CommonResult;
import cn.iocoder.lw.framework.common.util.object.BeanUtils;
import cn.iocoder.lw.module.core.controller.app.evaluation.vo.evaluation.*;
import cn.iocoder.lw.module.core.dal.dataobject.evaluation.CoreWorkOrderEvaluationDO;
import cn.iocoder.lw.module.core.service.evaluation.CoreWorkOrderEvaluationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.lw.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 工单评价")
@RestController
@RequestMapping("/core/work-order-evaluation")
@Validated
public class AppCoreWorkOrderEvaluationController {

    @Resource
    private CoreWorkOrderEvaluationService evaluationService;

    @PostMapping("/create")
    @Operation(summary = "创建工单评价")
    public CommonResult<Long> createEvaluation(@Valid @RequestBody AppCoreWorkOrderEvaluationCreateReqVO createReqVO) {
        return success(evaluationService.createEvaluationApp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单评价")
    public CommonResult<Boolean> updateEvaluation(@Valid @RequestBody AppCoreWorkOrderEvaluationUpdateReqVO updateReqVO) {
        evaluationService.updateEvaluationApp(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单评价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppCoreWorkOrderEvaluationRespVO> getEvaluation(@RequestParam("id") Long id) {
        CoreWorkOrderEvaluationDO evaluation = evaluationService.getEvaluation(id);
        return success(BeanUtils.toBean(evaluation, AppCoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/work-order/{workOrderId}")
    @Operation(summary = "根据工单编号获得评价")
    @Parameter(name = "workOrderId", description = "工单编号", required = true)
    public CommonResult<AppCoreWorkOrderEvaluationRespVO> getEvaluationByWorkOrderId(@PathVariable("workOrderId") Long workOrderId) {
        CoreWorkOrderEvaluationDO evaluation = evaluationService.getEvaluationByWorkOrderId(workOrderId);
        return success(BeanUtils.toBean(evaluation, AppCoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/my-initiated")
    @Operation(summary = "获得我发起的评价列表")
    public CommonResult<List<AppCoreWorkOrderEvaluationRespVO>> getMyInitiatedEvaluationList() {
        // TODO: 获取当前登录用户ID
        Long currentUserId = 1L; // 这里应该从SecurityContext获取
        List<CoreWorkOrderEvaluationDO> list = evaluationService.getEvaluationListByInitiatorId(currentUserId);
        return success(BeanUtils.toBean(list, AppCoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/my-received")
    @Operation(summary = "获得我收到的评价列表")
    public CommonResult<List<AppCoreWorkOrderEvaluationRespVO>> getMyReceivedEvaluationList() {
        // TODO: 获取当前登录用户ID（member用户ID）
        Long currentUserId = 1L; // 这里应该从SecurityContext获取
        List<CoreWorkOrderEvaluationDO> list = evaluationService.getEvaluationListByAssigneeId(currentUserId);
        return success(BeanUtils.toBean(list, AppCoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/my-unread")
    @Operation(summary = "获得我的未读评价列表")
    public CommonResult<List<AppCoreWorkOrderEvaluationRespVO>> getMyUnreadEvaluations() {
        // TODO: 获取当前登录用户ID（member用户ID）
        Long currentUserId = 1L; // 这里应该从SecurityContext获取
        List<CoreWorkOrderEvaluationDO> list = evaluationService.getUnreadEvaluationsByAssigneeId(currentUserId);
        return success(BeanUtils.toBean(list, AppCoreWorkOrderEvaluationRespVO.class));
    }

    @PutMapping("/mark-read/{id}")
    @Operation(summary = "标记评价为已读")
    @Parameter(name = "id", description = "评价编号", required = true)
    public CommonResult<Boolean> markAsRead(@PathVariable("id") Long id) {
        evaluationService.markAsRead(id);
        return success(true);
    }

    @PutMapping("/mark-all-read")
    @Operation(summary = "标记所有我的评价为已读")
    public CommonResult<Boolean> markAllMyEvaluationsAsRead() {
        // TODO: 获取当前登录用户ID（member用户ID）
        Long currentUserId = 1L; // 这里应该从SecurityContext获取
        List<CoreWorkOrderEvaluationDO> unreadEvaluations = evaluationService.getUnreadEvaluationsByAssigneeId(currentUserId);
        List<Long> ids = unreadEvaluations.stream()
                .map(CoreWorkOrderEvaluationDO::getId)
                .toList();
        if (!ids.isEmpty()) {
            evaluationService.batchMarkAsRead(ids);
        }
        return success(true);
    }

    @GetMapping("/my-statistics")
    @Operation(summary = "获得我的评价统计信息")
    public CommonResult<Object> getMyEvaluationStatistics() {
        // TODO: 获取当前登录用户ID（member用户ID）
        Long currentUserId = 1L; // 这里应该从SecurityContext获取
        return success(evaluationService.getEvaluationStatistics(currentUserId));
    }

    @GetMapping("/my-average-score")
    @Operation(summary = "获得我的平均评分")
    public CommonResult<Double> getMyAverageScore() {
        // TODO: 获取当前登录用户ID（member用户ID）
        Long currentUserId = 1L; // 这里应该从SecurityContext获取
        return success(evaluationService.getAverageScore(currentUserId));
    }

}